package com.thf.controller;

import com.alibaba.fastjson.JSON;
import com.thf.model.ResultData;
import com.thf.util.ResultTool;
import com.thf.util.Upload;
import io.minio.errors.*;
import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.rmi.ServerException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Controller
public class ImgUploadController{

    @RequestMapping("/upload")
    @ResponseBody
    protected String imgUpload(@RequestParam("file") MultipartFile file ) throws IOException, InvalidResponseException, RegionConflictException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
        System.out.println("==============================>>>"+file);
        String path = Upload.file(file);
        ResultData result = null ;
        if(!"".equals(path)){
           result = new ResultData(ResultTool.CODE_SUCCES,"",path);
        }else{
           result = new ResultData(ResultTool.CODE_ERROR);
        }
        //       反馈给前端--返回值
        String resJson = JSON.toJSONString(result);
        //System.out.println(resJson);
//
        return resJson ;
    }
}
